#!/bin/bash

# ===============================
# cm 控制程序管理脚本
# ===============================

JAR_FILE="cml-1.0.0.jar"
LOG_FILE="cml.log"
MEMORY_RATIO=60  # 最大占用系统内存比例 60%
PID_FILE="cml.pid"
function usage() {
    echo "用法: $0 {start|stop|restart|status|log|clear|help} [--cpu|--memory|--cpu-memory]"
    echo ""
    echo "命令说明:"
    echo "  start [--cpu|--memory|--cpu-memory]  启动程序，可选择CPU/内存/CPU+内存控制"
    echo "     #> ./cml.sh start --cpu             # 只占用 CPU"
    echo "     #> ./cml.sh start --memory          # 只占用内存"
    echo "     #> ./cml.sh start --cpu-memory      # CPU + 内存"
    echo "  stop                                 停止程序"
    echo "     #> ./cml.sh stop"
    echo "  restart [--cpu|--memory|--cpu-memory] 重启程序，可选择控制类型"
    echo "     #> ./cml.sh restart --cpu-memory"
    echo "  status                               查看程序运行状态"
    echo "  log                                  查看实时日志"
    echo "  clear                                清空日志"
    echo "  help                                 显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 start --cpu         # 启动只控制CPU占用"
    echo "  $0 start --memory      # 启动只控制内存占用"
    echo "  $0 start --cpu-memory  # 启动同时控制CPU和内存"
    exit 1
}

# -------------------------------
# 获取系统最大内存 (MB)
# -------------------------------
function get_max_memory() {
    TOTAL_MEM_MB=$(free -m | awk '/^Mem:/ {print $2}')
    MAX_MEM_MB=$(( TOTAL_MEM_MB * MEMORY_RATIO / 100 ))
    echo "$MAX_MEM_MB"
}

# -------------------------------
# 启动程序
# -------------------------------
function start() {
    CPU_FLAG=false
    MEMORY_FLAG=false

    for arg in "$@"; do
        case "$arg" in
            --cpu) CPU_FLAG=true ;;
            --memory) MEMORY_FLAG=true ;;
            --cpu-memory) CPU_FLAG=true; MEMORY_FLAG=true ;;
        esac
    done

    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if ps -p $PID > /dev/null 2>&1; then
            echo "程序已在运行, PID=$PID"
            return
        else
            rm -f "$PID_FILE"
        fi
    fi

    MAX_MEM_MB=$(get_max_memory)
    JVM_OPTS="-XX:MaxDirectMemorySize=${MAX_MEM_MB}M"
    APP_OPTS="--cpu.flag=$CPU_FLAG --memory.flag=$MEMORY_FLAG"

    echo "启动 JVM 参数: $JVM_OPTS"
    echo "应用参数: $APP_OPTS"

    nohup java $JVM_OPTS -jar $JAR_FILE $APP_OPTS > $LOG_FILE 2>&1 &
    PID=$!
    echo $PID > $PID_FILE
    echo "程序已启动, PID=$PID, 日志输出到 $LOG_FILE"
}

# -------------------------------
# 停止程序
# -------------------------------
function stop() {
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if ps -p $PID > /dev/null 2>&1; then
            kill $PID
            echo "程序已停止, PID=$PID"
        else
            echo "程序未运行, PID文件存在但进程不存在"
        fi
        rm -f "$PID_FILE"
    else
        echo "程序未运行"
    fi
}

# -------------------------------
# 重启程序
# -------------------------------
function restart() {
    stop
    sleep 1
    start "$@"
}

# -------------------------------
# 查看程序状态
# -------------------------------
function status() {
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if ps -p $PID > /dev/null 2>&1; then
            echo "程序正在运行, PID=$PID"
        else
            echo "程序未运行, PID文件存在但进程不存在"
        fi
    else
        echo "程序未运行"
    fi
}

# -------------------------------
# 查看日志
# -------------------------------
function log() {
    if [ -f "$LOG_FILE" ]; then
        tail -f "$LOG_FILE"
    else
        echo "日志文件不存在"
    fi
}

# -------------------------------
# 清空日志
# -------------------------------
function clear_log() {
    > "$LOG_FILE"
    echo "日志已清空"
}

# ===============================
# 主入口
# ===============================
COMMAND=$1
shift || true

case "$COMMAND" in
    start) start "$@" ;;
    stop) stop ;;
    restart) restart "$@" ;;
    status) status ;;
    log) log ;;
    clear) clear_log ;;
    help|*) usage ;;
esac
